<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="htmlId">
<head>
    <title>Coverage Report :: ScorePanel</title>
    <style type="text/css">
        @import "../../.css/coverage.css";
    </style>
</head>

<body>
<div class="header"></div>

<div class="content">
    <div class="breadCrumbs">
        [ <a href="../../index.html">all classes</a> ]
        [ <a href="../index.html">nl.tudelft.jpacman.ui</a> ]
    </div>

    <h1>Coverage Summary for Class: ScorePanel (nl.tudelft.jpacman.ui)</h1>

    <table class="coverageStats">
        <tr>
            <th class="name">Class</th>
            <th class="coverageStat
">
                Class, %
            </th>
            <th class="coverageStat
">
                Method, %
            </th>
            <th class="coverageStat
">
                Line, %
            </th>
        </tr>
        <tr>
            <td class="name">ScorePanel</td>
            <td class="coverageStat">
  <span class="percent">
    0%
  </span>
                <span class="absValue">
    (0/ 1)
  </span>
            </td>
            <td class="coverageStat">
  <span class="percent">
    0%
  </span>
                <span class="absValue">
    (0/ 5)
  </span>
            </td>
            <td class="coverageStat">
  <span class="percent">
    0%
  </span>
                <span class="absValue">
    (0/ 25)
  </span>
            </td>
        </tr>

    </table>

    <br/>
    <br/>


    <div class="sourceCode"><i>1</i>&nbsp;package nl.tudelft.jpacman.ui;
        <i>2</i>&nbsp;
        <i>3</i>&nbsp;import java.awt.GridLayout;
        <i>4</i>&nbsp;import java.util.LinkedHashMap;
        <i>5</i>&nbsp;import java.util.List;
        <i>6</i>&nbsp;import java.util.Map;
        <i>7</i>&nbsp;
        <i>8</i>&nbsp;import javax.swing.JLabel;
        <i>9</i>&nbsp;import javax.swing.JPanel;
        <i>10</i>&nbsp;
        <i>11</i>&nbsp;import nl.tudelft.jpacman.level.Player;
        <i>12</i>&nbsp;
        <i>13</i>&nbsp;/**
        <i>14</i>&nbsp; * A panel consisting of a column for each player, with the numbered players on
        <i>15</i>&nbsp; * top and their respective scores underneath.
        <i>16</i>&nbsp; *
        <i>17</i>&nbsp; * @author Jeroen Roosen
        <i>18</i>&nbsp; *
        <i>19</i>&nbsp; */
        <b class="nc"><i>20</i>&nbsp;public class ScorePanel extends JPanel {</b>
        <i>21</i>&nbsp;
        <i>22</i>&nbsp; /**
        <i>23</i>&nbsp; * Default serialisation ID.
        <i>24</i>&nbsp; */
        <i>25</i>&nbsp; private static final long serialVersionUID = 1L;
        <i>26</i>&nbsp;
        <i>27</i>&nbsp; /**
        <i>28</i>&nbsp; * The map of players and the labels their scores are on.
        <i>29</i>&nbsp; */
        <i>30</i>&nbsp; private final Map&lt;Player, JLabel&gt; scoreLabels;
        <i>31</i>&nbsp;
        <i>32</i>&nbsp; /**
        <i>33</i>&nbsp; * The default way in which the score is shown.
        <i>34</i>&nbsp; */
        <b class="nc"><i>35</i>&nbsp; public static final ScoreFormatter DEFAULT_SCORE_FORMATTER =</b>
        <b class="nc"><i>36</i>&nbsp; (Player player) -&gt; String.format(&quot;Score: %3d&quot;,
            player.getScore());</b>
        <i>37</i>&nbsp;
        <i>38</i>&nbsp; /**
        <i>39</i>&nbsp; * The way to format the score information.
        <i>40</i>&nbsp; */
        <b class="nc"><i>41</i>&nbsp; private ScoreFormatter scoreFormatter = DEFAULT_SCORE_FORMATTER;</b>
        <i>42</i>&nbsp;
        <i>43</i>&nbsp; /**
        <i>44</i>&nbsp; * Creates a new score panel with a column for each player.
        <i>45</i>&nbsp; *
        <i>46</i>&nbsp; * @param players
        <i>47</i>&nbsp; * The players to display the scores of.
        <i>48</i>&nbsp; */
        <i>49</i>&nbsp; public ScorePanel(List&lt;Player&gt; players) {
        <b class="nc"><i>50</i>&nbsp; super();</b>
        <b class="nc"><i>51</i>&nbsp; assert players != null;</b>
        <i>52</i>&nbsp;
        <b class="nc"><i>53</i>&nbsp; setLayout(new GridLayout(2, players.size()));</b>
        <i>54</i>&nbsp;
        <b class="nc"><i>55</i>&nbsp; for (int i = 1; i &lt;= players.size(); i++) {</b>
        <b class="nc"><i>56</i>&nbsp; add(new JLabel(&quot;Player &quot; + i, JLabel.CENTER));</b>
        <i>57</i>&nbsp; }
        <b class="nc"><i>58</i>&nbsp; scoreLabels = new LinkedHashMap&lt;&gt;();</b>
        <b class="nc"><i>59</i>&nbsp; for (Player player : players) {</b>
        <b class="nc"><i>60</i>&nbsp; JLabel scoreLabel = new JLabel(&quot;0&quot;, JLabel.CENTER);</b>
        <b class="nc"><i>61</i>&nbsp; scoreLabels.put(player, scoreLabel);</b>
        <b class="nc"><i>62</i>&nbsp; add(scoreLabel);</b>
        <b class="nc"><i>63</i>&nbsp; }</b>
        <i>64</i>&nbsp; }
        <i>65</i>&nbsp;
        <i>66</i>&nbsp; /**
        <i>67</i>&nbsp; * Refreshes the scores of the players.
        <i>68</i>&nbsp; */
        <i>69</i>&nbsp; protected void refresh() {
        <b class="nc"><i>70</i>&nbsp; for (Map.Entry&lt;Player, JLabel&gt; entry : scoreLabels.entrySet()) {</b>
        <b class="nc"><i>71</i>&nbsp; Player player = entry.getKey();</b>
        <b class="nc"><i>72</i>&nbsp; String score = &quot;&quot;;</b>
        <b class="nc"><i>73</i>&nbsp; if (!player.isAlive()) {</b>
        <b class="nc"><i>74</i>&nbsp; score = &quot;You died. &quot;;</b>
        <i>75</i>&nbsp; }
        <b class="nc"><i>76</i>&nbsp; score += scoreFormatter.format(player);</b>
        <b class="nc"><i>77</i>&nbsp; entry.getValue().setText(score);</b>
        <b class="nc"><i>78</i>&nbsp; }</b>
        <i>79</i>&nbsp; }
        <i>80</i>&nbsp;
        <i>81</i>&nbsp; /**
        <i>82</i>&nbsp; * Provide means to format the score for a given player.
        <i>83</i>&nbsp; */
        <i>84</i>&nbsp; public interface ScoreFormatter {
        <i>85</i>&nbsp;
        <i>86</i>&nbsp; /**
        <i>87</i>&nbsp; * Format the score of a given player.
        <i>88</i>&nbsp; * @param player The player and its score
        <i>89</i>&nbsp; * @return Formatted score.
        <i>90</i>&nbsp; */
        <i>91</i>&nbsp; String format(Player player);
        <i>92</i>&nbsp; }
        <i>93</i>&nbsp;
        <i>94</i>&nbsp; /**
        <i>95</i>&nbsp; * Let the score panel use a dedicated score formatter.
        <i>96</i>&nbsp; * @param scoreFormatter Score formatter to be used.
        <i>97</i>&nbsp; */
        <i>98</i>&nbsp; public void setScoreFormatter(ScoreFormatter scoreFormatter) {
        <b class="nc"><i>99</i>&nbsp; assert scoreFormatter != null;</b>
        <b class="nc"><i>100</i>&nbsp; this.scoreFormatter = scoreFormatter;</b>
        <i>101</i>&nbsp; }
        <i>102</i>&nbsp;}
    </div>
</div>

<div class="footer">

    <div style="float:right;">generated on 2023-04-25 16:45</div>
</div>
</body>
</html>
